import {ref, onMounted} from 'vue'
import {useRoute} from 'vue-router'
import {getCategoryFilter} from '@/network/category.js'
export function useGoodList() {
    const route = useRoute()
    const goodList = ref([])
    const data = ref({
        categoryId: route.params.id,
        page: 1,
        pageSize: 20,
        sortField: 'publishTime'
    })
    const getGoodList = async() => {
        const res = await getCategoryFilter(data.value)
        // console.log(res);
        goodList.value = res.result.items
    }

    onMounted(() => {
        getGoodList()
    })


    function tabChange() {
        console.log(data.value.sortField);
        data.value.page = 1
        getGoodList()
    }

    // 加载更多
    const disabled = ref(false)
   async function load() {
        // console.log('到底了');
        data.value.page++
        const res = await getCategoryFilter(data.value)
        goodList.value = [...goodList.value, ...res.result.items]
        // 加载完毕，停止监听
        if (res.result.items.length === 0) {
            disabled.value = true
        }
    }

  

    return {
        goodList,
        data,
        tabChange,
        load,
        disabled
    }
}